package com.zxj.camerademo.camera2;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.ImageFormat;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.SurfaceTexture;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CaptureFailure;
import android.hardware.camera2.CaptureRequest;
import android.hardware.camera2.CaptureResult;
import android.hardware.camera2.TotalCaptureResult;
import android.hardware.camera2.params.Face;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.media.Image;
import android.media.ImageReader;
import android.os.Handler;
import android.os.HandlerThread;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.util.Size;
import android.view.Surface;
import android.view.TextureView;
import android.widget.Toast;

import com.zxj.camerademo.utils.BitmapUtils;
import com.zxj.camerademo.view.AutoFitTextureView;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Camera2HelperFace {

    private static final String TAG = Camera2FaceActivity.class.getSimpleName();
    private Activity mActivity;
    private AutoFitTextureView mTextureView;
    private static final int PREVIEW_WIDTH = 1080;                                         //预览的宽度
    private static final int  PREVIEW_HEIGHT = 1440;                                       //预览的高度
    private static final int  SAVE_WIDTH = 720;                                            //保存图片的宽度
    private static final int  SAVE_HEIGHT = 1280;                                          //保存图片的高度

    private CameraManager mCameraManager;
    private ImageReader mImageReader = null;
    private CameraDevice mCameraDevice = null;
    private CameraCaptureSession mCameraCaptureSession = null;

    private String mCameraId = "0";
    private CameraCharacteristics mCameraCharacteristics;

    private int mCameraSensorOrientation = 0;                                            //摄像头方向
    private int mCameraFacing = CameraCharacteristics.LENS_FACING_BACK;              //默认使用后置摄像头
    private int mDisplayRotation ;  //手机方向
    private int mFaceDetectMode = CaptureResult.STATISTICS_FACE_DETECT_MODE_OFF;     //人脸检测模式

    private boolean canTakePic = true;                                                       //是否可以拍照
    private boolean canExchangeCamera = false;                                               //是否可以切换摄像头
    private boolean openFaceDetect = true;                                                   //是否开启人脸检测
    private Matrix mFaceDetectMatrix =new Matrix();                                            //人脸检测坐标转换矩阵
    private List<RectF> mFacesRect = new ArrayList<RectF>();                                         //保存人脸坐标信息
    private FaceDetectListener mFaceDetectListener = null;                         //人脸检测回调

    private Handler mCameraHandler;
    private HandlerThread handlerThread = new HandlerThread("CameraThread");

    private Size mPreviewSize = new Size(PREVIEW_WIDTH, PREVIEW_HEIGHT);                      //预览大小
    private Size mSavePicSize = new Size(SAVE_WIDTH, SAVE_HEIGHT);                            //保存图片大小


    public Camera2HelperFace(Activity activity, AutoFitTextureView textureView){
        this.mActivity = activity;
        this.mTextureView = textureView;
        init();
    }

    private void init() {
        mDisplayRotation = mActivity.getWindowManager().getDefaultDisplay().getRotation();

        handlerThread.start();
        mCameraHandler = new Handler(handlerThread.getLooper());

        mTextureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
                configureTransform(width, height);
                initCameraInfo();
            }

            @Override
            public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
                configureTransform(width, height);
            }

            @Override
            public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
                releaseCamera();
                return true;
            }

            @Override
            public void onSurfaceTextureUpdated(SurfaceTexture surface) {

            }
        });
    }

    /**
     * 初始化
     */
    private void initCameraInfo() {
        mCameraManager = (CameraManager) mActivity.getSystemService(Context.CAMERA_SERVICE);
        String[] cameraIdList = null;
        try {
            cameraIdList = mCameraManager.getCameraIdList();
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
        if (cameraIdList == null || cameraIdList.length == 0) {
            Toast.makeText(mActivity, "没有可用相机", Toast.LENGTH_SHORT).show();
            return;
        }

        for (String id : cameraIdList) {
            CameraCharacteristics cameraCharacteristics = null;
            try {
                cameraCharacteristics = mCameraManager.getCameraCharacteristics(id);
            } catch (CameraAccessException e) {
                e.printStackTrace();
            }
            int facing = cameraCharacteristics.get(CameraCharacteristics.LENS_FACING);

            if (facing == mCameraFacing) {
                mCameraId = id;
                mCameraCharacteristics = cameraCharacteristics;
            }

            Log.e("Camera2Helper", "设备中的摄像头 " + id);
        }
        int supportLevel = mCameraCharacteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL);
        if (supportLevel == CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY) {
            Toast.makeText(mActivity, "相机硬件不支持新特性", Toast.LENGTH_SHORT).show();
        }

        //获取摄像头方向
        mCameraSensorOrientation = mCameraCharacteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
        //获取StreamConfigurationMap，它是管理摄像头支持的所有输出格式和尺寸
        StreamConfigurationMap configurationMap = mCameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);


        Size[] savePicSize = configurationMap.getOutputSizes(ImageFormat.JPEG);          //保存照片尺寸
        Size[] previewSize = configurationMap.getOutputSizes(SurfaceTexture.class); //预览尺寸

        boolean exchange = exchangeWidthAndHeight(mDisplayRotation, mCameraSensorOrientation);

        mSavePicSize = getBestSize(
                exchange ? mSavePicSize.getHeight() : mSavePicSize.getWidth(),
                exchange ? mSavePicSize.getWidth() : mSavePicSize.getHeight(),
                exchange ? mSavePicSize.getHeight() : mSavePicSize.getWidth(),
                exchange ? mSavePicSize.getWidth() : mSavePicSize.getHeight(),
                Arrays.asList(savePicSize));

        mPreviewSize = getBestSize(
                exchange ? mPreviewSize.getHeight() : mPreviewSize.getWidth(),
                exchange ? mPreviewSize.getWidth() : mPreviewSize.getHeight(),
                exchange ? mTextureView.getHeight() : mTextureView.getWidth(),
                exchange ? mTextureView.getWidth() : mTextureView.getHeight(),
                Arrays.asList(previewSize));

        mTextureView.getSurfaceTexture().setDefaultBufferSize(mPreviewSize.getWidth(),mPreviewSize.getHeight());

        Log.e(TAG,"预览最优尺寸 ："+mPreviewSize.getWidth()+" * "+mPreviewSize.getHeight()+", 比例  "+(float)mPreviewSize.getWidth() / mPreviewSize.getHeight());
        Log.e(TAG,"保存图片最优尺寸 ："+mSavePicSize.getWidth()+" * "+mSavePicSize.getHeight()+", 比例  "+(float)mSavePicSize.getWidth() / mSavePicSize.getHeight());

        //根据预览的尺寸大小调整TextureView的大小，保证画面不被拉伸
        int orientation = mActivity.getResources().getConfiguration().orientation;
        if (orientation == Configuration.ORIENTATION_LANDSCAPE)
            mTextureView.setAspectRatio(mPreviewSize.getWidth(), mPreviewSize.getHeight());
        else
            mTextureView.setAspectRatio(mPreviewSize.getHeight(), mPreviewSize.getWidth());

        mImageReader = ImageReader.newInstance(mSavePicSize.getWidth(), mSavePicSize.getHeight(), ImageFormat.JPEG, 1);
        mImageReader.setOnImageAvailableListener(onImageAvailableListener, mCameraHandler);

        if (openFaceDetect)
            initFaceDetect(); //初始化人脸检测相关参数

        openCamera();
    }

    /**
     * 初始化人脸检测相关信息
     */
    private void initFaceDetect() {
        int faceDetectCount = mCameraCharacteristics.get(CameraCharacteristics.STATISTICS_INFO_MAX_FACE_COUNT);    //同时检测到人脸的数量
        int[] faceDetectModes = mCameraCharacteristics.get(CameraCharacteristics.STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES);  //人脸检测的模式

        for (int faceDetectMode : faceDetectModes) {
            if(faceDetectMode == CaptureRequest.STATISTICS_FACE_DETECT_MODE_FULL){
                mFaceDetectMode = CaptureRequest.STATISTICS_FACE_DETECT_MODE_FULL;
            }else if(faceDetectMode == CaptureRequest.STATISTICS_FACE_DETECT_MODE_SIMPLE){
                mFaceDetectMode = CaptureRequest.STATISTICS_FACE_DETECT_MODE_SIMPLE;
            }else {
                mFaceDetectMode = CaptureRequest.STATISTICS_FACE_DETECT_MODE_OFF;
            }
        }

        if (mFaceDetectMode == CaptureRequest.STATISTICS_FACE_DETECT_MODE_OFF) {
            Toast.makeText(mActivity,"相机硬件不支持人脸检测",Toast.LENGTH_SHORT).show();
            return;
        }

        Rect activeArraySizeRect = mCameraCharacteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE); //获取成像区域
        float scaledWidth = (float) mPreviewSize.getWidth() / activeArraySizeRect.width();
        float scaledHeight = (float) mPreviewSize.getHeight() / activeArraySizeRect.height();
        boolean mirror = mCameraFacing == CameraCharacteristics.LENS_FACING_FRONT;

        mFaceDetectMatrix.setRotate(mCameraSensorOrientation);
        mFaceDetectMatrix.postScale(mirror ? -scaledWidth : scaledWidth, scaledHeight);

        if (exchangeWidthAndHeight(mDisplayRotation, mCameraSensorOrientation)) {
            mFaceDetectMatrix.postTranslate(mPreviewSize.getHeight(), mPreviewSize.getWidth());
        }

        Log.e(TAG,"成像区域:"+activeArraySizeRect.width() +" * "+activeArraySizeRect.height() +"，比例："+ (float)activeArraySizeRect.width()/activeArraySizeRect.height());
        Log.e(TAG,"预览区域:"+mPreviewSize.getWidth() +" * "+mPreviewSize.getHeight() +"，比例："+ (float)mPreviewSize.getWidth()/mPreviewSize.getHeight());

        for (int faceDetectMode : faceDetectModes) {
            Log.e(TAG,"支持的人脸检测模式:"+faceDetectMode);
        }

        Log.e(TAG,"同时检测到人脸的数量 :"+faceDetectCount);
    }

    /**
     * 打开相机
     */
    private void openCamera(){
        if (ContextCompat.checkSelfPermission(mActivity, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(mActivity,"没有相机权限！",Toast.LENGTH_SHORT).show();
            return;
        }
        try {
            mCameraManager.openCamera(mCameraId, new CameraDevice.StateCallback() {
                @Override
                public void onOpened(@NonNull CameraDevice camera) {
                    Log.d(TAG,"onOpened");
                    mCameraDevice = camera;
                    createCaptureSession(camera);
                }

                @Override
                public void onDisconnected(@NonNull CameraDevice camera) {
                    Log.d(TAG,"onDisconnected");
                }

                @Override
                public void onError(@NonNull CameraDevice camera, int error) {
                    Log.d(TAG,"onError:"+error);
                    Toast.makeText(mActivity,"打开相机失败！"+error,Toast.LENGTH_SHORT).show();
                }
            },mCameraHandler);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }

    private ImageReader.OnImageAvailableListener onImageAvailableListener = new ImageReader.OnImageAvailableListener() {
        @Override
        public void onImageAvailable(ImageReader reader) {
            Log.e("zuo","onImageAvailableListener------");
            Image image = reader.acquireLatestImage();
            ByteBuffer byteBuffer = image.getPlanes()[0].getBuffer();
            byte[] byteArray = new byte[byteBuffer.remaining()];
            byteBuffer.get(byteArray);
            reader.close();
            Log.e("zuo","onImageAvailableListener======");
            BitmapUtils.savePic(byteArray, mCameraSensorOrientation == 270, new OnSavePicState() {
                @Override
                public void onSuccess(final String savedPath, final long time) {
                    Log.e("zuo","onSuccess------");
                    mActivity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(mActivity,"图片保存成功！ 保存路径："+savedPath+",耗时："+time,Toast.LENGTH_SHORT).show();
                        }
                    });
                }

                @Override
                public void onFailed(final String msg) {
                    Log.e("zuo","onFailed------"+msg);
                    mActivity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(mActivity,"图片保存失败！ "+msg,Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            });
        }
    };

    /**
     * 创建预览会话
     */
    private void createCaptureSession(CameraDevice cameraDevice) {
        CaptureRequest.Builder captureRequestBuilder = null;
        try {
            captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
            Surface surface = new Surface(mTextureView.getSurfaceTexture());
            captureRequestBuilder.addTarget(surface);  // 将CaptureRequest的构建器与Surface对象绑定在一起
            captureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);      // 闪光灯
            captureRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE); // 自动对焦
            //设置人脸检测
            if(openFaceDetect && mFaceDetectMode != CaptureRequest.STATISTICS_FACE_DETECT_MODE_OFF){
                captureRequestBuilder.set(CaptureRequest.STATISTICS_FACE_DETECT_MODE, CameraCharacteristics.STATISTICS_FACE_DETECT_MODE_SIMPLE);//人脸检测
            }
            final CaptureRequest.Builder finalCaptureRequestBuilder = captureRequestBuilder;

            // 为相机预览，创建一个CameraCaptureSession对象
            cameraDevice.createCaptureSession(Arrays.asList(surface, mImageReader.getSurface()), new CameraCaptureSession.StateCallback() {
                @Override
                public void onConfigured( @NonNull CameraCaptureSession session) {
                    mCameraCaptureSession = session;
                    try {
                        session.setRepeatingRequest(finalCaptureRequestBuilder.build(), mCaptureCallBack, mCameraHandler);
                    } catch (CameraAccessException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onConfigureFailed( @NonNull CameraCaptureSession session) {
                    Toast.makeText(mActivity,"开启预览会话失败！",Toast.LENGTH_SHORT).show();
                }
            },mCameraHandler);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }

    private CameraCaptureSession.CaptureCallback mCaptureCallBack = new CameraCaptureSession.CaptureCallback() {

        @Override
        public void onCaptureCompleted( @NonNull CameraCaptureSession session, @NonNull CaptureRequest request,@NonNull TotalCaptureResult result) {
            super.onCaptureCompleted(session, request, result);
            if (openFaceDetect && mFaceDetectMode != CaptureRequest.STATISTICS_FACE_DETECT_MODE_OFF)
                handleFaces(result);
            canExchangeCamera = true;
            canTakePic = true;
        }

        @Override
        public void onCaptureFailed( @NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull CaptureFailure failure) {
            super.onCaptureFailed(session, request, failure);
            Log.d(TAG,"onCaptureFailed");
            Toast.makeText(mActivity,"开启预览失败！",Toast.LENGTH_SHORT).show();
        }
    };

    /**
     * 处理人脸信息
     */
    private void handleFaces(TotalCaptureResult result){
        final Face[] faces = result.get(CaptureResult.STATISTICS_FACES);
        mFacesRect.clear();

        for (Face face : faces) {
            Rect bounds = face.getBounds();
            int left = bounds.left;
            int top = bounds.top;
            int right = bounds.right;
            int bottom = bounds.bottom;

            RectF rawFaceRect = new RectF(left,top, right, bottom);
            mFaceDetectMatrix.mapRect(rawFaceRect);
            RectF resultFaceRect;
            if(mCameraFacing == CaptureRequest.LENS_FACING_FRONT){
                resultFaceRect = rawFaceRect;
            }else {
                resultFaceRect = new RectF(rawFaceRect.left, rawFaceRect.top - mPreviewSize.getWidth(), rawFaceRect.right, rawFaceRect.bottom - mPreviewSize.getWidth());
            }

            mFacesRect.add(resultFaceRect);
            Log.d(TAG,"原始人脸位置:"+bounds.width()+"*"+bounds.height()+",left:"+bounds.left + ",top:"+bounds.top +",right:"+ bounds.right+ ",bottom:"+bounds.bottom+"分数: "+face.getScore());
            Log.d(TAG,"转换后人脸位置:"+resultFaceRect.width()+"*"+resultFaceRect.height()+",left:"+resultFaceRect.left+ ",top:"+resultFaceRect.top+",right:"+resultFaceRect.right+ ",bottom:"+resultFaceRect.bottom+" 分数:"+face.getScore());
        }

        mActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if(mFaceDetectListener != null)
                    mFaceDetectListener.onFaceDetect(Arrays.asList(faces), mFacesRect);
            }
        });
        Log.d(TAG,"onCaptureCompleted  检测到 "+faces.length+"张人脸");
    }

    /**
     * Configures the necessary [android.graphics.Matrix] transformation to `mTextureView`.
     * This method should be called after the camera preview size is determined in
     * setUpCameraOutputs and also the size of `mTextureView` is fixed.
     *
     * @param viewWidth  The width of `mTextureView`
     * @param viewHeight The height of `mTextureView`
     */
    private void configureTransform(int viewWidth,int viewHeight){
        int rotation = mActivity.getWindowManager().getDefaultDisplay().getRotation();
        Matrix matrix = new Matrix();
        RectF viewRect = new RectF(0f, 0f, viewWidth, viewHeight);
        RectF bufferRect = new RectF(0f,0f,mPreviewSize.getHeight(),mPreviewSize.getWidth());
        float centerX = viewRect.centerX();
        float centerY = viewRect.centerY();
        if(Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
            bufferRect.offset(centerX - bufferRect.centerX(),centerY - bufferRect.centerY());
            matrix.setRectToRect(viewRect,bufferRect,Matrix.ScaleToFit.FILL);
            int scale = Math.max(viewHeight / mPreviewSize.getHeight(), viewWidth / mPreviewSize.getWidth());
            matrix.postScale(scale,scale,centerX,centerY);
            matrix.postRotate(180f,centerX,centerY);
        }else if(Surface.ROTATION_180 == rotation) {
            matrix.postRotate(180f, centerX, centerY);
        }
        mTextureView.setTransform(matrix);
        Log.e(TAG,"viewWidth:"+viewWidth+",viewHeight:"+viewHeight);

    }

    /**
     * 根据提供的屏幕方向 [displayRotation] 和相机方向 [sensorOrientation] 返回是否需要交换宽高
     */
    private boolean exchangeWidthAndHeight(int displayRotation, int sensorOrientation) {
        boolean exchange = false;
        switch (displayRotation) {
            case Surface.ROTATION_0:
            case Surface.ROTATION_180:
                if (sensorOrientation == 90 || sensorOrientation == 270) {
                    exchange = true;
                }
                break;
            case Surface.ROTATION_90:
            case Surface.ROTATION_270:
                if (sensorOrientation == 0 || sensorOrientation == 180) {
                    exchange = true;
                }
                break;
            default:
                Log.e(TAG, "Display rotation is invalid: " + displayRotation);
                break;
        }

        Log.e(TAG, "屏幕方向  " + displayRotation);
        Log.e(TAG, "相机方向  " + sensorOrientation);
        return exchange;
    }

    /**
     * 根据提供的参数值返回与指定宽高相等或最接近的尺寸
     *
     * @param targetWidth  目标宽度
     * @param targetHeight
     * @param maxWidth
     * @param maxHeight
     * @param sizeList
     * @return
     */
    private Size getBestSize(int targetWidth, int targetHeight, int maxWidth, int maxHeight, List<Size> sizeList) {
        List<Size> bigEnough = new ArrayList<>();     //比指定宽高大的Size列表
        List<Size> notBigEnough = new ArrayList<>();  //比指定宽高小的Size列表

        for (Size size : sizeList) {
            //宽<=最大宽度  &&  高<=最大高度  &&  宽高比 == 目标值宽高比
            if (size.getWidth() <= maxWidth && size.getHeight() <= maxHeight
                    && size.getWidth() == size.getHeight() * targetWidth / targetHeight) {

                if (size.getWidth() >= targetWidth && size.getHeight() >= targetHeight)
                    bigEnough.add(size);
                else
                    notBigEnough.add(size);
            }
            Log.e(TAG, "系统支持的尺寸: " + size.getWidth() + " * "+size.getHeight() + " ,  比例 ：" + (float)size.getWidth() / size.getHeight());
        }

        Log.e(TAG, "最大尺寸 ：" + maxWidth + " * " + maxHeight + ", 比例 ：" + (float)targetWidth / targetHeight);
        Log.e(TAG, "目标尺寸 ：" + targetWidth + " * " + targetHeight + ", 比例 ：" + (float)targetWidth / targetHeight);

        //选择bigEnough中最小的值  或 notBigEnough中最大的值

        if (bigEnough.size() > 0) {
            return Collections.min(bigEnough, new CompareSizesByArea());
        } else if (notBigEnough.size() > 0) {
            return Collections.max(notBigEnough, new CompareSizesByArea());
        } else {
            return sizeList.get(0);
        }
    }

    /**
     * 拍照
     */
    public void takePic() {
        if (mCameraDevice == null || !mTextureView.isAvailable() || !canTakePic) return;

        CaptureRequest.Builder captureRequestBuilder = null;
        try {
            captureRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
            captureRequestBuilder.addTarget(mImageReader.getSurface());

            captureRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE); // 自动对焦
            captureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);     // 闪光灯
            captureRequestBuilder.set(CaptureRequest.JPEG_ORIENTATION, mCameraSensorOrientation);      //根据摄像头方向对保存的照片进行旋转，使其为"自然方向"
            if(mCameraCaptureSession != null) {
                mCameraCaptureSession.capture(captureRequestBuilder.build(), null, mCameraHandler);
//                mCameraCaptureSession.stopRepeating();
            }else {
                Toast.makeText(mActivity,"拍照异常！",Toast.LENGTH_SHORT).show();
            }

        } catch (CameraAccessException e) {
            e.printStackTrace();
        }

    }

    private class CompareSizesByArea implements Comparator<Size> {

        @Override
        public int compare(Size size1, Size size2) {
            return java.lang.Long.signum(size1.getWidth() * size1.getHeight() - size2.getWidth() * size2.getHeight());
        }
    }

    public void exchangeCamera() {
        if (mCameraDevice == null || !canExchangeCamera || !mTextureView.isAvailable()) return;
        Log.e("zuo","切换摄像头前："+mCameraFacing);
        if(mCameraFacing == CameraCharacteristics.LENS_FACING_FRONT){
            mCameraFacing = CameraCharacteristics.LENS_FACING_BACK;
        }else {
            mCameraFacing = CameraCharacteristics.LENS_FACING_FRONT;
        }
        Log.e("zuo","切换摄像头后："+mCameraFacing);
        mPreviewSize = new Size(PREVIEW_WIDTH, PREVIEW_HEIGHT); //重置预览大小
        releaseCamera();
        initCameraInfo();
    }

    public void releaseCamera() {
        if(mCameraCaptureSession != null){
            mCameraCaptureSession.close();
            mCameraCaptureSession = null;
        }

        if(mCameraDevice != null){
            mCameraDevice.close();
            mCameraDevice = null;
        }

        if(mImageReader != null){
            mImageReader.close();
            mImageReader = null;
        }

        canExchangeCamera = false;
    }

    public void releaseThread() {
        handlerThread.quitSafely();
        try {
            handlerThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    interface FaceDetectListener {
        void onFaceDetect(List<Face> faces , List<RectF> facesRect);
    }

    public void setFaceDetectListener(FaceDetectListener listener) {
        this.mFaceDetectListener = listener;
    }
}
